<?php

class Init
{
    protected $link = null;
    // 定义sql文件路径
    protected $path = __DIR__ . '/../assets/sql' . '/';

    public function __construct($config)
    {
        $this->connect($config);
    }
    // 数据库连接
    protected function connect($config)
    {
        if (is_null($this->link)) {
            // 连接mysql数据库，但是未选择数据库
            try {
                $dsn = sprintf('mysql:host=%s', $config['host']);
                $this->link = new PDO($dsn, $config['user'], $config['password'], [PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION]);
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }
    }

    public function execSqlFile(string $filename)
    {
        // 读sql文件
        $sql = $this->readSqlFile($filename);
        // 执行sql
        $this->link->exec($sql);
    }

    // 读取sql文件函数
    protected function readSqlFile($filename): string
    {
        $filed = $this->path . $filename;
        return file_get_contents($filed);
    }

    public function closeSqlLink()
    {
        $this->link = null;
    }
}

class Fetch
{
    protected $link = null;

    public function __construct($config)
    {
        $this->connect($config);
    }
    // 数据库连接
    protected function connect($config)
    {
        if (is_null($this->link)) {
            // 连接mysql数据库，并选择数据库
            try {
                $dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8', $config['host'], $config['dbname'], $config['charset']);
                $this->link = new PDO($dsn, $config['user'], $config['password'], [PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION]);
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }
    }

    // 根据表名查询所有的数据
    public function fetchByTable(string $tableName): array
    {
        $arr = [];
        $query = $this->link->query("select * from $tableName limit 20");
        $arr = $query->fetchAll(PDO::FETCH_ASSOC);
        return $arr;
    }
    // 根据id查询单条数据
    public function fetchUserById(string $user_id): array
    {
        $arr = [];
        $query = $this->link->query("select * from user WHERE user_id =  $user_id");
        $arr = $query->fetch(PDO::FETCH_ASSOC);
        return $arr;
    }

    public function fetchGoodsById(string $goods_id): array
    {
        $arr = [];
        $query = $this->link->query("select * from goods WHERE goods_id =  $goods_id");
        $arr = $query->fetch(PDO::FETCH_ASSOC);
        return $arr;
    }

    public function fetchIndentById(string $order_id): array
    {
        $arr = [];
        $query = $this->link->query("select * from indent WHERE order_id =  $order_id");
        $arr = $query->fetch(PDO::FETCH_ASSOC);
        return $arr;
    }

    // 根据id删除数据
    public function delByID($tableName, $fieldID, $id)
    {
        // 预准备语句
        $sql = "DELETE FROM $tableName WHERE $fieldID = :id";
        $sth = $this->link->prepare($sql);
        // 参数绑定防止sql注入 
        $sth->execute(['id' => $id]);
    }

    // 添加用户
    public function insertUserData(string $tableName, array $array)
    {
        $sql = "INSERT INTO
        $tableName(
            `username`,
            `password`,
            `user_email`,
            `user_age`,
            `user_sex`,
            `user_tel`,
            `user_address`
        )
    VALUES (
            :username,
            :password,
            :user_email,
            :user_age,
            :user_sex,
            :user_tel,
            :user_address
        )";
        $sth = $this->link->prepare($sql);
        $sth->execute(['username' => $array['username'], 'password' => $array['password'], 'user_email' => $array['user_email'], 'user_age' => $array['user_age'], 'user_sex' => $array['user_sex'], 'user_tel' => $array['user_tel'], 'user_address' => $array['user_address']]);
    }

    // 修改用户数据
    public function modifyUser(array $array)
    {
        $sql = "UPDATE
        user SET
        `username` = :username,
        `password` = :password,
        `user_email` = :user_email,
        `user_age` = :user_age,
        `user_sex` = :user_sex,
        `user_tel` = :user_tel,
        `user_address` = :user_address
        WHERE user_id = :user_id
        ";
        $sth = $this->link->prepare($sql);
        $sth->execute(['username' => $array['username'], 'password' => $array['password'], 'user_email' => $array['user_email'], 'user_age' => $array['user_age'], 'user_sex' => $array['user_sex'], 'user_tel' => $array['user_tel'], 'user_address' => $array['user_address'], 'user_id' => $array['user_id']]);
    }
    // 添加商品
    public function insertGoodsData(string $tableName, array $array)
    {
        $sql = "INSERT INTO
        $tableName (
            `goods_name`,
            `goods_price`,
            `goods_total`,
            `goods_status`,
            `goods_keywords`,
            `goods_description`,
            `goods_add_time`
        )
    VALUES (
            :goods_name,
            :goods_price,
            :goods_total,
            :goods_status,
            :goods_keywords,
            :goods_description,
            :goods_add_time
        )";
        $sth = $this->link->prepare($sql);
        $sth->execute(['goods_name' => $array['goods_name'], 'goods_price' => $array['goods_price'], 'goods_total' => $array['goods_total'], 'goods_status' => $array['goods_status'], 'goods_keywords' => $array['goods_keywords'], 'goods_description' => $array['goods_description'], 'goods_add_time' => $array['goods_add_time']]);
    }

    // 修改商品数据
    public function modifyGoods(array $array)
    {
        $sql = "UPDATE
        goods SET
        `goods_name` = :goods_name,
        `goods_price` = :goods_price,
        `goods_total` = :goods_total,
        `goods_status` = :goods_status,
        `goods_keywords` = :goods_keywords,
        `goods_description` = :goods_description,
        `goods_add_time` = :goods_add_time
        WHERE goods_id = :goods_id
        ";
        $sth = $this->link->prepare($sql);
        $sth->execute(['goods_name' => $array['goods_name'], 'goods_price' => $array['goods_price'], 'goods_total' => $array['goods_total'], 'goods_status' => $array['goods_status'], 'goods_keywords' => $array['goods_keywords'], 'goods_description' => $array['goods_description'], 'goods_add_time' => $array['goods_add_time'], 'goods_id' => $array['goods_id']]);
    }

    // 添加订单
    public function insertIndentData(string $tableName, array $array)
    {
        $sql = "INSERT INTO
        $tableName (
            `order_no`,
            `order_status`,
            `order_amount_total`,
            `logistics_fee`,
            `pay_channel`,
            `payment_time`,
            `delivery_time`
        )
    VALUES (
            :order_no,
            :order_status,
            :order_amount_total,
            :logistics_fee,
            :pay_channel,
            :payment_time,
            :delivery_time
        )";
        $sth = $this->link->prepare($sql);
        $sth->execute(['order_no' => $array['order_no'], 'order_status' => $array['order_status'], 'order_amount_total' => $array['order_amount_total'], 'logistics_fee' => $array['logistics_fee'], 'pay_channel' => $array['pay_channel'], 'payment_time' => $array['payment_time'], 'delivery_time' => $array['delivery_time']]);
    }

    // 修改用户数据
    public function modifyIndent(array $array)
    {

        $sql = "UPDATE
        indent SET
        `order_no` = :order_no,
        `order_status` = :order_status,
        `order_amount_total` = :order_amount_total,
        `logistics_fee` = :logistics_fee,
        `pay_channel` = :pay_channel,
        `payment_time` = :payment_time,
        `delivery_time` = :delivery_time
        WHERE order_id = :order_id
        ";
        $sth = $this->link->prepare($sql);
        $sth->execute(['order_no' => $array['order_no'], 'order_status' => $array['order_status'], 'order_amount_total' => $array['order_amount_total'], 'logistics_fee' => $array['logistics_fee'], 'pay_channel' => $array['pay_channel'], 'payment_time' => $array['payment_time'], 'delivery_time' => $array['delivery_time'], 'order_id' => $array['order_id']]);
    }
}